package com.cloud.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cloud.mall.dto.CategoryQueryDto;
import com.cloud.mall.dto.CategorySaveDto;
import com.cloud.mall.dto.product.CategoryDto;
import com.cloud.mall.entity.Category;
import com.cloud.mall.mapper.CategoryMapper;
import com.cloud.mall.result.GeneralResponse;
import com.cloud.mall.result.PageResponse;
import com.cloud.mall.service.ICategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 梁二东
 * @Date: 2025/1/8 10:16
 * @Description:
 **/
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public GeneralResponse<List<CategoryDto>> getCategories(CategoryQueryDto queryDto) {
        if (queryDto.getPageIndex() != null && queryDto.getPageSize() != null) {
            PageHelper.startPage(queryDto.getPageIndex(), queryDto.getPageSize());
            List<Category> categories = categoryMapper.selectListByQueryDto(queryDto);
            PageInfo<Category> pageInfo = new PageInfo<>(categories);
            List<CategoryDto> categoryDtos = BeanUtil.copyToList(categories, CategoryDto.class);
            PageResponse<List<CategoryDto>> pageResponse = new PageResponse<List<CategoryDto>>();
            pageResponse.setPage(queryDto.getPageIndex());
            pageResponse.setSize(queryDto.getPageSize());
            pageResponse.setTotal(pageInfo.getTotal());
            pageResponse.setCode(0);
            pageResponse.setData(categoryDtos);
            return pageResponse;
        } else {
            List<Category> categories = categoryMapper.selectListByQueryDto(queryDto);
            List<CategoryDto> categoryDtos = BeanUtil.copyToList(categories, CategoryDto.class);
            if (queryDto.isTree()) {
                List<CategoryDto> categoryTree = buildTree(categoryDtos);
                return GeneralResponse.success(categoryTree);
            }
            return GeneralResponse.success(categoryDtos);
        }
    }

    @Override
    public GeneralResponse<String> saveCategory(CategorySaveDto saveDto) {
        return null;
    }

    public List<CategoryDto> buildTree(List<CategoryDto> categories) {
        Map<Long, CategoryDto> categoryMap = new HashMap<>();
        List<CategoryDto> tree = new ArrayList<>();

        // 首先将所有的类别放入一个映射中，方便查找
        for (CategoryDto category : categories) {
            categoryMap.put(category.getId(), category);
            category.setChildren(new ArrayList<>()); // 初始化子节点列表
        }

        // 然后构建树结构
        for (CategoryDto category : categories) {
            Long parentId = category.getParentId();
            if (parentId == 0) {
                // 如果没有父ID，则它是根节点
                tree.add(category);
            } else {
                // 否则，将其添加到父节点的子列表中
                CategoryDto parent = categoryMap.get(parentId);
                if (parent != null) {
                    parent.getChildren().add(category);
                }
            }
        }

        return tree;
    }
}
